/*
* @(#)ListDmsArchiveDetailAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsArchiveDetail;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.dao.DmsArchiveDetailDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
 * <p>Class Name:       ListDmsArchiveDetailAction.java    </p>
 * <p>Description:      The list action class for ListArchive.jsp</p>
 *
 *    @author           Angus Shiu
 *    @company          DCIVision Limited
 *    @creation date    18/09/2003
 *    @version          $Revision: 1.10.38.1 $
 */

public class ListDmsArchiveDetailAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.10.38.1 $";


  /**
   *  Constructor - Creates a new instance of ListArchiveAction and define the default listName.
   */
  public ListDmsArchiveDetailAction() {
    super();
    this.setListName("dmsArchiveDetailList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsArchiveDetailDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_ARCHIVE);
  }
  
  /**
   * execute - Override the parent function.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException  {
    ListDmsArchiveDetailForm listForm = (ListDmsArchiveDetailForm)form;
    String[] arr = listForm.getSelectedDoc();
    ArrayList tmpList = new ArrayList();

    for (int i=0; arr!=null && i<arr.length; i++) {
      StringTokenizer tokenizer = new StringTokenizer(arr[i], ",");
      while (tokenizer.hasMoreTokens()) {
        tmpList.add(tokenizer.nextToken());
      }
    }
    arr = (String[]) tmpList.toArray(new String[]{});
    listForm.setSelectedDoc(arr);
    return super.execute(mapping, form, request, response);
  }
  
  /**
   * getListData
   *
   * Override the parent's function. Purpose in create the default personal folder when non-exists,
   * and load the dmsDocument list.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    List originalList = new ArrayList();
    List resultList = new ArrayList();
    Connection conn = null;
    DocumentRetrievalManager docRetrievalManager = null;
    try {
      conn = this.getConnection(request);
      docRetrievalManager = new DocumentRetrievalManager(this.getSessionContainer(request), conn);
      ListDmsArchiveDetailForm listForm=(ListDmsArchiveDetailForm)form;
      DmsArchiveDetailDAObject dmsArchiveDetailDAO=new DmsArchiveDetailDAObject(this.getSessionContainer(request),conn);
      originalList=dmsArchiveDetailDAO.getListByArchiveID(TextUtility.parseIntegerObj(listForm.getArchiveID()));
      resultList=dmsArchiveDetailDAO.getListByArchiveIDSegmentID(form);
      boolean allowSpecificVersion = Boolean.getBoolean(SystemParameterFactory.getSystemParameter(SystemParameterConstant.
        DMS_ARCHIVE_ALLOW_RESTORE_SPECIFIC_VERSION));
      if (allowSpecificVersion) {
        for (int i = 0; i < originalList.size(); i++) {
          DmsArchiveDetail archiveDetail = (DmsArchiveDetail)originalList.get(i);
          List tmpList = docRetrievalManager.getArchivedListByDocumentIDArchiveID(archiveDetail.getDocumentID(), archiveDetail.getArchiveID());
          archiveDetail.setVersionList(tmpList);
          resultList.add(archiveDetail);
        }
      }
        request.setAttribute("originalList",originalList);
        request.setAttribute(this.m_sListName, resultList);
    } catch (Exception e) { 
      log.error(e,e); 
    } finally { 
      conn = null; 
      docRetrievalManager.release();
    }
  }

}
